package com.yzjs.wx;


import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

public class WxPaySignature {
    public WxPaySignature() {
    }

    public static String sign(Map<String, String> params, String signKey) {
        SortedMap<String, String> sortedMap = new TreeMap(params);
        StringBuilder toSign = new StringBuilder();
        Iterator var4 = sortedMap.keySet().iterator();

        while(var4.hasNext()) {
            String key = (String)var4.next();
            String value = (String)params.get(key);
            if (StringUtils.isNotEmpty(value) && !"sign".equals(key) && !"key".equals(key)) {
                toSign.append(key).append("=").append(value).append("&");
            }
        }

        toSign.append("key=").append(signKey);
        return DigestUtils.md5Hex(toSign.toString()).toUpperCase();
    }

    public static Boolean verify(Map<String, String> params, String privateKey) {
        String sign = sign(params, privateKey);
        return sign.equals(params.get("sign"));
    }
}
